﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace UILayoutEditor
{
    class StringTableEntry
    {
        public StringTableEntry(string key, string value)
        {
            this.Key = key;
            this.Value = value;
        }
        public string Key;
        public string Value;
    }
    class StringTable
    {
        List<StringTableEntry> table;
        public string GetValue(string key)
        {
            for (int i = 0; i < table.Count; i++)
			{
                if (table[i].Key == key)
                {
                    return table[i].Value;
                }
			}
            return null;
        }
        public void Load(string filename)
        {
            //Load the file
            FileStream fs = File.OpenRead(filename);
            BinaryReader br = new BinaryReader(fs);

            //Console.Write(br.ReadUInt32().ToString());
            UInt32 entryCount = br.ReadUInt32();

            int length;

            table = new List<StringTableEntry>();
            char[] stringCharArray;
            List<char> stringCharList;

            List<string> tempList = new List<string>();

            for (int i = 0; i < entryCount; i++)
            {
                length = br.ReadUInt16();

                stringCharArray = new char[length];
                for (int j = 0; j < length; j++)
                {
                    stringCharArray[j] = br.ReadChar();
                }
                stringCharList = stringCharArray.ToList<char>();
                stringCharList.RemoveAt(stringCharList.Count - 1);
                string hashMapKey = new string(stringCharList.ToArray());
                //br.ReadByte();
                length = br.ReadUInt16();

                stringCharArray = new char[length];
                for (int j = 0; j < length; j++)
                {
                    stringCharArray[j] = (char)br.ReadByte();
                }
                stringCharList = stringCharArray.ToList<char>();
                //stringCharList.RemoveAt(stringCharList.Count - 2);
                stringCharList.RemoveAt(stringCharList.Count - 1);
                string hashMapValue = new string(stringCharList.ToArray());

                if (!tempList.Contains(hashMapValue))
                    tempList.Add(hashMapValue);
                //br.ReadByte();
                //br.ReadBytes(8);

                table.Add(new StringTableEntry(hashMapKey, hashMapValue));                
            }

            br.Close();
            fs.Close();
        }

    }
}
